package com.vince.hulk.ogmo
{
import com.vince.hulk.character.Character;
import com.vince.hulk.character.LargeCharacter;
import com.vince.hulk.character.MediumCharacter;
import com.vince.hulk.character.SmallCharacter;
import com.vince.hulk.mechanism.Door;
import com.vince.hulk.mechanism.Fan;
import com.vince.hulk.mechanism.GameButtonLarge;
import com.vince.hulk.mechanism.GameButtonMedium;
import com.vince.hulk.mechanism.GameButtonSmall;
import com.vince.hulk.mechanism.IMechanism;
import com.vince.hulk.mechanism.MovableBlock;
import com.vince.hulk.mechanism.Switch;
import com.vince.hulk.states.PlayState;

import org.flixel.FlxG;
import org.flixel.FlxGroup;
import org.flixel.FlxObject;
import org.flixel.FlxState;
import org.flixel.FlxTilemap;

/**
 * author yangwz
 */		
public class OgmoLevel
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>OgmoLevel</code> instance.
	 * 
	 */
	public function OgmoLevel(data:String)
	{
		xml = new XML(data);
	}
	//==========================================================================
	//  Variables
	//==========================================================================
	//==========================================================================
	//  Properties
	//==========================================================================
	public var xml:XML;
	//==========================================================================
	//  Methods
	//==========================================================================
	public function loadTiles(flxTileMap:FlxTilemap, clz:Class, tileWidth:int, tileHeight:int):void
	{
		var tile:String = xml.Tiles.toString();
		flxTileMap.loadMap(tile.replace(new RegExp("-1", "g"), "0"), clz, tileWidth, tileHeight);
	}
	
	public function loadSprites(arr:Array, group:FlxGroup) : void
	{
		var characterData:XML;
		var pointX:int = 0;
		var pointY:int = 0;
		var character:Character = null;
		var switchId:int = 0;
		var switchMechanism:IMechanism = null;
		var switchData:XML = null;
		var smallSwitch:Switch = null;
		var buttonid:int = 0;
		var buttonMechanism:IMechanism = null;
		var buttonData:XML = null;
		var buttonSmall:GameButtonSmall = null;
		var buttonMedium:GameButtonMedium = null;
		var buttonLarge:GameButtonLarge = null;
		var block:MovableBlock = null;
		var direction:uint = 0;
		var fan:Fan = null;
		var door:Door = null;
		var state:FlxState = FlxG.state;
		for each (characterData in xml.Objects.children())
		{
			if (characterData.@type == "character")
			{
				pointX = characterData.@x;
				pointY = characterData.@y;
				if (characterData.@size == 1)
				{
					character = new SmallCharacter(pointX, pointY);
					new SmallCharacter(pointX, pointY).y = character.y - character.height / 2;
					arr.push(character);
					state.add(character);
				}
				else if (characterData.@size == 2)
				{
					character = new MediumCharacter(pointX, pointY);
					new MediumCharacter(pointX, pointY).y = character.y - character.height / 2;
					arr.push(character);
					state.add(character);
				}
				else if (characterData.@size == 3)
				{
					character = new LargeCharacter(pointX, pointY);
					new LargeCharacter(pointX, pointY).y = character.y - character.height / 2;
					arr.push(character);
					state.add(character);
				}
				continue;
			}
			if (characterData.@type == "switch")
			{
				switchId = characterData.@buttonid;
				switchMechanism = null;
				for each (switchData in xml.Objects.children())
				{
					
					if (switchData.@type != "switch" && switchData.@type != "button" && switchData.@buttonid == switchId)
					{
						if (switchData.@type == "door")
						{
							if (switchMechanism != null)
							{
								Door(switchMechanism).setAfterActivateDoor(new Door(switchData.@x, switchData.@y));
								break;
							}
							else
							{
								switchMechanism = new Door(switchData.@x, switchData.@y);
								state.add(Door(switchMechanism));
								PlayState(state).doors.add(Door(switchMechanism));
							}
							continue;
						}
						if (switchData.@type == "platform")
						{
						}
					}
				}
				smallSwitch = new Switch(characterData.@x, characterData.@y - 5, switchMechanism);
				state.add(smallSwitch);
				group.add(smallSwitch);
				continue;
			}
			if (characterData.@type == "button")
			{
				buttonid = characterData.@buttonid;
				buttonMechanism = null;
				for each (buttonData in xml.Objects.children())
				{
					
					if (buttonData.@type != "switch" && buttonData.@type != "button" && buttonData.@buttonid == buttonid)
					{
						if (buttonData.@type == "door")
						{
							if (buttonMechanism != null)
							{
								Door(buttonMechanism).setAfterActivateDoor(new Door(buttonData.@x, buttonData.@y));
								break;
							}
							else
							{
								buttonMechanism = new Door(buttonData.@x, buttonData.@y);
								state.add(Door(buttonMechanism));
								PlayState(state).doors.add(Door(buttonMechanism));
							}
							continue;
						}
						if (buttonData.@type == "platform")
						{
						}
					}
				}
				if (characterData.@size == 1)
				{
					buttonSmall = new GameButtonSmall(characterData.@x, characterData.@y, buttonMechanism);
					state.add(buttonSmall);
					PlayState(state).collidableMechanisms.add(buttonSmall);
				}
				else if (characterData.@size == 2)
				{
					buttonMedium = new GameButtonMedium(characterData.@x, characterData.@y, buttonMechanism);
					state.add(buttonMedium);
					PlayState(state).collidableMechanisms.add(buttonMedium);
				}
				else if (characterData.@size == 3)
				{
					buttonLarge = new GameButtonLarge(characterData.@x, characterData.@y, buttonMechanism);
					state.add(buttonLarge);
					PlayState(state).collidableMechanisms.add(buttonLarge);
				}
				continue;
			}
			if (characterData.@type == "movableblock")
			{
				block = new MovableBlock(characterData.@x, characterData.@y);
				state.add(block);
				PlayState(state).blocks.add(block);
				continue;
			}
			if (characterData.@type == "fan" && !("@buttonid" in characterData))
			{
				if (characterData.@direction == "left")
				{
					direction = FlxObject.LEFT;
				}
				else if (characterData.@direction == "right")
				{
					direction = FlxObject.RIGHT;
				}
				else if (characterData.@direction == "up")
				{
					direction = FlxObject.UP;
				}
				else
				{
					direction = FlxObject.DOWN;
				}
//				fan = new Fan(characterData.@size, direction, 130, characterData.@x, characterData.@y);
				fan = new Fan(characterData.@x, characterData.@y, characterData.@size, direction, 130);
				state.add(fan);
				group.add(fan);
				continue;
			}
			if (characterData.@type == "door" && !("@buttonid" in characterData))
			{
				door = new Door(characterData.@x, characterData.@y);
				state.add(door);
				continue;
			}
			if (characterData.@type == "platform" && !("buttonid" in characterData))
			{
			}
		}
	}
}
}